package com.youseu.taoliving.commodity.service.impl;

import com.youseu.taoliving.commodity.entity.AttrEntity;
import com.youseu.taoliving.commodity.service.AttrService;
import com.youseu.taoliving.commodity.vo.AttrGroupWithAttrsVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youseu.common.utils.PageUtils;
import com.youseu.common.utils.Query;

import com.youseu.taoliving.commodity.dao.AttrgroupDao;
import com.youseu.taoliving.commodity.entity.AttrgroupEntity;
import com.youseu.taoliving.commodity.service.AttrgroupService;

import javax.annotation.Resource;


@Service("attrgroupService")
public class AttrgroupServiceImpl extends ServiceImpl<AttrgroupDao, AttrgroupEntity> implements AttrgroupService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrgroupEntity> page = this.page(
                new Query<AttrgroupEntity>().getPage(params),
                new QueryWrapper<AttrgroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {
        //根据搜索框查询值和categoryId查询
        String condition = (String) params.get("key");
        QueryWrapper<AttrgroupEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isBlank(condition)){
            //传进来的可能是数字或者字符 根据id或者name进行查询
            // wrapper.eq("id",condition).or().like("name",condition);
            wrapper.and((obj) -> {
                obj.eq("id",condition).or().like("name",condition);
            });
        }
        //规定categoryId为0不加入该查询条件
        if(categoryId != 0){
            //不为0则加入该条件
            //后台查询语句最终是WHERE (id = ? OR name LIKE ? AND category_id = ?)
            wrapper.eq("category_id",categoryId);
        }

        IPage<AttrgroupEntity> page = this.page(
                new Query<AttrgroupEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Resource
    private AttrService attrService;

    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCategoryId(Long categoryId) {
        //根据categoryId关联的属性组
        List<AttrgroupEntity> attrgroupEntities = this.list(new QueryWrapper<AttrgroupEntity>().eq("category_id", categoryId));
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = attrgroupEntities.stream().map((item) -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            //将item中属性拷贝到attrGroupWithAttrsVo
            BeanUtils.copyProperties(item, attrGroupWithAttrsVo);
            //AttrService中有根据属性组id 返回对应基本属性的方法
            List<AttrEntity> relationAttr = attrService.getRelationAttr(item.getId());
            attrGroupWithAttrsVo.setAttrs(relationAttr);
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());

        return attrGroupWithAttrsVos;
    }

}